<?php

namespace App\Admin\Controllers\Record;

use App\Http\Controllers\Controller;
use App\Admin\Models\Record\Treatment;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Widgets\Table;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;
use Ixudra\Curl\Facades\Curl;
use DB;

class TreatmentController extends Controller
{
	use HasResourceActions;
	
    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('顾客资料档案表')
            ->description('列表')
            ->body($this->grid());
    }
    
    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('流水记录')
            ->description('详细')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('')
            ->description('编辑')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('账本')
            ->description('手工帐')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Treatment);
        $grid->model()->where('sId', '=', Admin::user()->sId);
        $grid->rtId('ID');
        $grid->muCode('客户姓名');
        $grid->rtType('疗程类型');
        $grid->rtDate('日期');
        $grid->rtName('疗程名称');
        $grid->rtOperator('经办人');
        $grid->createTime('创建时间');
        $grid->createUser('创建人');

		$grid->filter(function($filter){

		    // 去掉默认的id过滤器
		    $filter->disableIdFilter();

		});
		
		$grid->actions(function ($actions) {
			
		    $actions->disableDelete();
		    $actions->disableEdit();
			$actions->disableView();
		});
		
		// 禁用新增按钮
//		$grid->disableCreateButton();

		// 默认展开筛选框
		$grid->expandFilter();
		
		// 禁用行选择checkbox
		$grid->disableRowSelector();
		
		// 禁用导出
		$grid->disableExport();
        return $grid;
    }
    
    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
    	$walletTrade = new walletTrade();
        $show = new Show($walletTrade->findOrFail($id));

        //$show->spId('ID');
        $show->spType('参数类型');
        $show->spKey('参数名称');
        $show->spValue('参数值');
        $show->spMemo('参数备注');
        $show->spSort('参数排序');
		$show->panel()
			->tools(function ($tools) {
//				$tools->disableEdit();
//				$tools->disableList();
				$tools->disableDelete();
		});;
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::Form(Treatment::class, function (Form $form) {
			
			$now = date('YmdHis', time());
			$today = date('Y-m-d', time());
			$form->select('muCode', '会员')->options($this->getUserSelect());
			$rtType = array();
			$rtType['面部'] = '面部';
			$rtType['身体'] = '身体';
			$rtType['仪器'] = '仪器';
			$form->select('rtType', '疗程类型')->options($rtType);
			$form->date('rtDate', '日期')->default($today);
			$form->text('rtName', '疗程名称');
			$form->text('rtOperator', '经办人');
			
			$form->saved(function (Form $form) {
			
			    $rtId = $form->model()->rtId;
				DB::table('record_treatment')->where('rtId', $rtId)->update(['createUser'=> Admin::user()->name, 'sId' => $sId = Admin::user()->sId]);
			});
			
            $form->tools(function (Form\Tools $tools) {

			    // 去掉`列表`按钮
//			    $tools->disableList();
				
			    // 去掉`删除`按钮
			    $tools->disableDelete();
			
			    // 去掉`查看`按钮
			    $tools->disableView();
			
			    // 添加一个按钮, 参数可以是字符串, 或者实现了Renderable或Htmlable接口的对象实例
//			    $tools->add('<a class="btn btn-sm btn-danger"><i class="fa fa-trash"></i>&nbsp;&nbsp;delete</a>');
			});
			
			$form->footer(function ($footer) {

//			    // 去掉`重置`按钮
//			    $footer->disableReset();
//			
//			    // 去掉`提交`按钮
//			    $footer->disableSubmit();
			
			    // 去掉`查看`checkbox
			    $footer->disableViewCheck();
			
			    // 去掉`继续编辑`checkbox
			    $footer->disableEditingCheck();
			
			    // 去掉`继续创建`checkbox
			    $footer->disableCreatingCheck();
			
			});
        });
    }
    
    // 获取会员下拉框
    private function getUserSelect(){
    	$sId = Admin::user()->sId;
		$host = config('admin.dataApiHost');
		$url = $host.'/member/getList?mcKey='.$sId.'&page=1&perPage=999';

		$data = Curl::to($url)->get();

		$data = json_decode($data, true);
		$infoList = $data['data']['data'];

		$selectData = array();
		foreach($infoList as $info){
			$selectData[$info['muCode']] = $info['muNickName'];
		}
		
		return $selectData;
    }
}
